package com.example.his.api.db.dao;

import cn.hutool.core.map.MapUtil;
import com.example.his.api.db.pojo.CheckupResultEntity;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class CheckupResultDao {
  @Resource
  private MongoTemplate mongoTemplate;
  
  public boolean insert(String uuid, List <Map> checkup) {
    
    CheckupResultEntity entity = new CheckupResultEntity();
    entity.setUuid(uuid);
    entity.setCheckup(checkup);
    entity.setPlace(new ArrayList <>() {{
    }});
    entity.setResult(new ArrayList <>() {{
    }});
    //    添加新记录
    entity = mongoTemplate.insert(entity);
    return entity.get_id() != null;
  }
  
  //  根据uuid查询体检结果的主键值
  public String searchIdByUuid(String uuid) {
    Criteria criteria = Criteria.where("uuid").is(uuid);
    Query query = new Query(criteria);
    CheckupResultEntity entity = mongoTemplate.findOne(query, CheckupResultEntity.class);
    return entity.get_id();
    
  }
  
  //  根据体检科室查找体检内容
  public List <Map> searchCheckupByPlace(String uuid, String place) {
    Aggregation aggregation = Aggregation.newAggregation(Aggregation.project("checkup", "uuid"), Aggregation.unwind("$checkup"), Aggregation.match(Criteria.where("uuid").is(uuid).and("checkup.place").is(place)));
    AggregationResults <HashMap> res = mongoTemplate.aggregate(aggregation, "checkup_result", HashMap.class);
    ArrayList <Map> list = new ArrayList();
    res.forEach(item -> {
      HashMap map = (HashMap)item.get("checkup");
      list.add(map);
    });
    return list;
  }
  
  //  检查当前科室有没有给当前体检录入过结果
  public boolean hasAlreadyCheckup(String uuid, String place) {
    
    Aggregation aggregation = Aggregation.newAggregation(Aggregation.project("result", "uuid"), Aggregation.unwind("$result"), Aggregation.match(Criteria.where("uuid").is(uuid).and("result.place").is(place)));
    
    AggregationResults <HashMap> res = mongoTemplate.aggregate(aggregation, "checkup_result", HashMap.class);
    if(res.getMappedResults().size() > 0) {
      return true;
    } else {
      return false;
    }
  }
  
  //  添加体检结果
  public void addResult(String uuid, String place, Map map) {
    Criteria criteria = Criteria.where("uuid").is(uuid);
    Query query = new Query(criteria);
    //    根据uuid查找记录
    CheckupResultEntity entity = mongoTemplate.findOne(query, CheckupResultEntity.class);
    List <Map> resultList = entity.getResult();
    List <String> placeList = entity.getPlace();
    
    //    判断是否存在该科室的体检结果
    if(placeList.contains(place)) {
      for(int i = 0; i < placeList.size(); i++) {
        Map item = resultList.get(i);
        String temp = MapUtil.getStr(item, "place");
        if(place.equals(temp)) {
          //   用新的结果覆盖旧结果
          resultList.set(i, map);
          break;
        }
      }
    } else {
      //      添加体检过的科室
      placeList.add(place);
      //      添加体检结果
      resultList.add(map);
    }
    //    更新数据
    mongoTemplate.save(entity);
  }
  
  //  根据体检结果主键查询体检结果
  public CheckupResultEntity searchById(String id) {
    CheckupResultEntity entity = mongoTemplate.findById(id, CheckupResultEntity.class);
    return entity;
  }
}
